<template>
  <div>
    <el-dialog
      :title="detail.onlineVersion?'升级小程序':'发布小程序'"
      :visible.sync="dialogVisible"
      width="700px"
      :close-on-click-modal="false"
      @close="close"
    >
      <div v-if="detail" v-loading="loading" class="dialog-container">
        <el-form ref="form" label-width="120px" size="small">
          <el-form-item
            label="当前版本号："
          >{{ detail.onlineVersion?detail.onlineVersion.versionNum:'未发布' }}</el-form-item>
          <el-form-item
            :label="detail.onlineVersion?'升级版本号：':'发布版本号：'"
          >{{ detail.canUpdateVersion.versionNum }}</el-form-item>
          <el-form-item label="版本更新说明：">{{ detail.canUpdateVersion.updateMsg }}</el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" round :disabled="loading" @click="hide">取 消</el-button>
        <el-button
          size="small"
          round
          type="primary"
          :disabled="loading"
          @click="handleSubmit"
        >{{ detail.onlineVersion?'升 级':'发 布' }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { updateWxappVersion } from '@/api/wechat'
export default {
  data() {
    return {
      resolve: null,
      reject: null,
      dialogVisible: null,
      loading: false,
      type: '',
      detail: ''
    }
  },
  methods: {
    open(detail) {
      this.detail = detail
      if (detail.onlineVersion) {
        this.type = 'update'
      } else {
        this.type = 'add'
      }
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },
    show() {
      this.dialogVisible = true
    },
    close() {
      this.loading = false
      this.type = null
      this.baseInfo = null
      this.resolve = null
      this.reject = null
    },

    hide() {
      this.dialogVisible = false
    },

    handleSubmit() {
      this.loading = true
      updateWxappVersion(this.detail.appId)
        .then(res => {
          this.$notify({
            title: '成功',
            message: '提交审核成功',
            type: 'success',
            duration: 2000,
            offset: 40
          })
          this.resolve && this.resolve()
          this.hide()
        })
        .catch(err => {})
        .finally(() => {
          this.loading = false
        })
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
  .dialog-container ::v-deep .el-form-item {
    margin-bottom: 0;
  }
</style>
